import locale from "@/utils/i18n";
const { t: $t } = locale.global
/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path: string) {
    return /^(https?:|mailto:|tel:)/.test(path)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUsername(str: string) {
    const valid_map = ['admin', 'editor']
    return valid_map.indexOf(str.trim()) >= 0
}

/**
 * @param {string} url
 * @returns {Boolean}
 */
export function validURL(url: string) {
    const reg = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
    return reg.test(url)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validLowerCase(str: string) {
    const reg = /^[a-z]+$/
    return reg.test(str)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUpperCase(str: string) {
    const reg = /^[A-Z]+$/
    return reg.test(str)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validAlphabets(str: string) {
    const reg = /^[A-Za-z]+$/
    return reg.test(str)
}

/**
 * @param {string} email
 * @returns {Boolean}
 */
export function validEmail(email: string) {
    const reg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
    return reg.test(email)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function isString(rule: any, str: any) {
    if (str) {
        return Promise.resolve()
    } else if (!str) {
        return Promise.reject($t('不能为空,请输入'))
    }
}

/**
 * 下拉框校验是否为空
 * @param {string} str
 * @returns {Boolean}
 */
export function selectValidate(rule: any, str: any) {
    if (str) {
        // return callback()
        return Promise.resolve()
    } else if (!str) {
        // callback($t('不能为空,请选择'))
        return Promise.reject($t('不能为空,请选择'))
    }
}

/**
 * @param {Array} arg
 * @returns {Boolean}
 */
export function isArray(arg: any) {
    if (typeof Array.isArray === 'undefined') {
        return Object.prototype.toString.call(arg) === '[object Array]'
    }
    return Array.isArray(arg)
}

/**
 * @param {string} password
 * @returns {Boolean}
 */
export function isStrongPassword(password: string) {
    const reg = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[~_@^!&#$%\*-\+=:,\\?\[\]\{}]).{8,16}$/;
    return reg.test(password)
}

/**
 * @param {string} email
 * @returns {Boolean}
 */
export function isEmail(rule: any, email: any) {
    const reg = new RegExp("^[A-Za-z0-9]+([._\\-]*[A-Za-z0-9])*@([A-Za-z0-9]+[-A-Za-z0-9]*[A-Za-z0-9]+.){1,63}[A-Za-z0-9]+$");
    if (!reg.test(email)) {
        // callback(new Error($t('请输入正确邮箱')));
        return Promise.reject(new Error($t('请输入正确邮箱')));
    } else if (!email) {
        // callback(new Error($t('邮箱不能为空!')));
        return Promise.reject(new Error($t('邮箱不能为空')));
    }
    return Promise.resolve();
}


/**
 * 只校验输入的值
 * @param {string} email
 * @returns {Boolean}
 */
export function isEmailNoEmpty(rule: any, email: any) {
    const reg = new RegExp("^[A-Za-z0-9]+([._\\-]*[A-Za-z0-9])*@([A-Za-z0-9]+[-A-Za-z0-9]*[A-Za-z0-9]+.){1,63}[A-Za-z0-9]+$");
    if (!email) {
        return Promise.resolve()
    }
    else if (email && !reg.test(email)) {
        return Promise.reject(new Error($t('请输入正确邮箱')))
    }
    return Promise.resolve();
}
/**
 * 正则判断是否为空或者为undefined
 * @param obj 
 * @returns 
 */
export function isEmptyOrUndefined(obj: any) {
    const reg = /^\s*$/
    if (obj === undefined) {
        return true
    }
    return reg.test(obj);
}

/**
 * 判断中国、阿拉伯、美国、英国手机号
 * @param phone 电话
 * @returns 
 */
export function isPhone(phone: any, value: any, callback: Function) {
    const reg = /^((\+|00)86)?1[3-9]\d{9}$/
    if (!reg.test(value)) {
        callback(new Error($t('电话号码格式错误')));
        return;
    } else if (!value) {
        callback(new Error($t('电话号码不能为空')));
    }
    return callback();
}

/**
 * 校验所有传入的参数
 * @param rule 规则传入
 * @param value 传入的值
 * @param callback 回调函数
 */
export function validateDyFormParams(rule: any, value: any, callback: Function) {
    const reg = /^\s*$/
    if (!value) {
        callback(new Error($t('请输入')));
        return
    }
}
export default { isArray, isString, isStrongPassword, isEmail, isEmptyOrUndefined, isPhone }
